package cn.wolfcode.shop.seckillserver.web.controller.frontend;

import cn.wolfcode.shop.redis.RedisService;
import cn.wolfcode.shop.seckillserver.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.seckillserver.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by wolfcode-lanxw
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/get")
    public void getVerifyCode(String uuid, HttpServletResponse response) throws IOException {

        String verifyCode = VerifyCodeImgUtil.generateVerifyCode();
        int result = VerifyCodeImgUtil.calc(verifyCode);

        redisService.set(SeckillKeyPrefix.VERIFYCODE_RESULT,uuid,result);

        BufferedImage img = VerifyCodeImgUtil.createVerifyCodeImg(verifyCode);

        ImageIO.write(img,"JPEG",response.getOutputStream());

    }
}
